import sys

class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        nRow = len(grid)
        nCol = len(grid[0])
        dp = [[sys.maxsize for _ in range(nCol+1)] for _ in range(nRow+1)]
        dp[-1][0] = 0
        dp[0][-1] = 0
        for r in range(nRow):
            for c in range(nCol):
                dp[r][c] = min(dp[r-1][c], dp[r][c-1]) + grid[r][c]
        return dp[nRow-1][nCol-1]